<!--
 * @Author: your name
 * @Date: 2021-04-10 10:27:43
 * @LastEditTime: 2021-06-04 09:30:08
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ebc-admin\src\views\basics\GeneralSet\Short.vue
-->
<template>
  <div class="no-padding">
    <!-- 列表 -->
    <tab-list
      ref="list"
      :searchs="searchs"
      :list-hedar="listHedar"
      :params="params"
      otherShow
      :api-function="apiFunction"
      @add-list="openEditBox()"
      @init-params="initParams"
    >
      <template slot="operate" slot-scope="scope">
        <el-button v-show="scope.value.sendStatus != '1'" type="text" @click="openEditBox(scope.value.id)">编 辑</el-button>
        <el-button v-show="scope.value.sendStatus == '1'" type="text" @click="openEditBox(scope.value.id, true)">查看</el-button>
        <el-button v-show="scope.value.sendStatus != '1'" type="text" @click="deleteItem(scope.value.id)">删 除</el-button>
      </template>
    </tab-list>
    <!-- 短信模板编辑弹窗 -->
    <maeesge-box ref="box" @submit="submit" title="编辑短信消息">
      <edit-from-list ref="edit" :params="editParams" :editList="editList" />
    </maeesge-box>
  </div>
</template>

<script>
// 组件
import tabList from '@/components/admin/tab-list.vue'
import maeesgeBox  from '@/components/admin/maeesge-box';
import editFromList from '@/components/admin/edit-from-list.vue';

// // 数据
import { configData, createEditData } from './listConfig'

// 接口
import { notifySmsAll, notifyTemplateSel, notifySmsDetail, notifySmsEdit, notifySmsDel, tagDownList } from '@/api/news.js'
import { asyncApiFun, openMessageBox } from '@/utils/utils'

export default {
  name: 'Short',
  components: {
    tabList,
    maeesgeBox,
    editFromList
  },
  data() {
    return {
      ...configData(),
      editList: [],
      params: {},
      editParams: null,
      templateList: [],
      tagDownList: [],
      apiFunction: notifySmsAll
    }
  },
  created() {
    this.getTemplateList();
    this.getTagDownList();
  },
  methods: {
    async getTemplateList() {
      const [res] = await asyncApiFun(notifyTemplateSel);
      if (res) {
        this.templateList = res.data.map(item => ({value: item.id, title: item.tempName, content: item.tempContent}));
      }
    },
    async getTagDownList() {
      const [res] = await asyncApiFun(tagDownList);
      if (res) {
        this.tagDownList = res.data.map(item => ({value: item.tagId, title: item.tagName}));
      }
    },
    async submit() {
      const [res] = await asyncApiFun(notifySmsEdit, this.$refs.edit.ruleFrom());
      if (res) {
        this.$message.success('操作成功')
        this.$refs.box.close();
        this.$refs.list.getList();
      }
    },
    deleteItem(id) {
      openMessageBox({
        content: '删除数据后，将不可恢复，是否确认？',
        title: '删除数据'
      }, async boole => {
        if (boole) {
          const [res] = await asyncApiFun(notifySmsDel, id)
          if (res) this.$refs.list.getList()
        }
      })
    },
    async openEditBox(id, isRed) {
      let _detail = null;
      if (id) {
        this.editParams = {id};
        const [res] = await asyncApiFun(notifySmsDetail, id);
        if (res) {
          _detail = res.data;
        }
      }
      this.editList = createEditData(_detail, this.templateList, isRed, this.tagDownList);
      this.$refs.box.open();
    },
    // 初始化列表请求参数
    initParams(param) {
      this.params = param
    }
  }

}

</script>

<style lang="scss" scoped>

</style>
